package cn.beforedark.yygh.userinfo.controller;


import cn.beforedark.yygh.common.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * /user/userinfo/code
 */
@RestController
@RequestMapping("/user/userinfo/code")
public class CodeController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 随机生成验证码
     *
     * @param phone 手机号
     * @return ok
     */
    @GetMapping("/send/{phone}")
    public R sendCode(@PathVariable("phone") String phone) {
        // 六位随机验证码
        int randomCode = new Random().nextInt(1_000_000);
        String formattedCode = String.format("%06d", randomCode);
        redisTemplate.opsForValue().set("code:phone:" + phone, formattedCode, 5, TimeUnit.MINUTES);
        return R.ok();
    }


}
